package com.jhws.sys.cms.service;

import com.jhws.sys.cms.bo.CommGroup;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.common.base.bo.Global;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommGroupService {
    @Resource
    private ITblUserDimService IUserService;
    @Resource
    private BaseDao BaseDao;
    @Resource
    private ITblCommDimService commDimService;
    @Resource
    private CmsRedisService redisService;

    /**
     * 保存分组信息
     *
     * @param commGroup 分组信息
     * @return id
     */
    public String saveCommGroup(CommGroup commGroup) throws Exception {
        if (StringUtils.isNotEmpty(commGroup.getGroupId())) {
            BaseDao.h().update(commGroup);
        } else {
            BaseDao.h().save(commGroup);
        }
        redisService.emptyRedisCommGroup();
        return commGroup.getGroupId();
    }

    /**
     * @param Id 组ID
     * @return 资源
     */
    public CommGroup findCommGroupInfoById(String Id) {
        if (StringUtils.isEmpty(Id)) {
            return null;
        }
        return BaseDao.get(CommGroup.class,Id);
    }

    /**
     * 查询小区分组信息
     *
     * @param param map
     * @return
     */
    public Map<String, Object> findCommGroupList(Map param) throws Exception {
        String whereClause = " 1=1 ";
        TblUserDimBo userBo = IUserService.currentUser();
        //admin权限可以查看所有分组
        if (!SecurityUtils.getSubject().hasRole(Global.ROLE_SUPPER_ADMIN)) {
            whereClause += " and creator='" + userBo.getLogName() + "'";
        }
        boolean isPage = MapUtils.getBooleanValue(param, "isPage");
        boolean isCommInfo = MapUtils.getBooleanValue(param, "isCommInfo");
        String parentGroupId = MapUtils.getString(param, "parentGroupId", "");
        if (StringUtils.isNotEmpty(parentGroupId)) {
            whereClause += " and parent_group_id ='" + parentGroupId + "'";
        }

        String sql = "select * from cms_comm_group where " + whereClause;
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list;
        if (isPage) {
            Page p = BaseDao.queryPage(sql, Map.class);
            list = p.getList();
            result.put("size", p.getTotalNumber());
        } else {
            list = BaseDao.j().queryForList(sql);
        }
        if (isCommInfo) {
            for (Map<String, Object> map : list) {
                String commCode = MapUtils.getString(map, "comm_code", "");
                if (StringUtils.isNotEmpty(commCode)) {
                    TblCommDimBo commDimBo = commDimService.findTblCommDimByCommCode(commCode);
                    map.put("commInfo", commDimBo);
                }
            }
        }
        result.put("list", list);
        result.put("ret_code", 1);
        return result;
    }

    /**
     * 条件查询小区分组信息
     *
     * @param where 条件
     * @return
     */
    public List<CommGroup> findCommGroupListByWhere(String where) {
        return BaseDao.findList("from CommGroup " + where);
    }

    /**
     * 根据小区查询小区分组信息
     *
     * @param commCode 小区
     * @return
     */
    public List<CommGroup> findCommGroupListByCommCode(String commCode) {
        List<CommGroup> groupList = redisService.getRedisCommGroup(commCode);
        if (groupList == null || groupList.size() <= 0) {
            groupList = findCommGroupListByWhere(" where comm_code='" + commCode + "'");
            if (groupList.size() > 0) {
                redisService.addRedisCommGroup(commCode, groupList);
            }
        }
        return groupList;
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    public void deleteCommGroup(String id) throws Exception {
        BaseDao.deleteByWhere("CommGroup", "id = '" + id + "'");
        redisService.emptyRedisCommGroup();
    }

    /**
     * 条件删除
     *
     * @param where
     */
    public void deleteCommGroupByWhere(String where) throws Exception {
        BaseDao.j().execute("delete from cms_comm_group " + where);
        redisService.emptyRedisCommGroup();
    }
}
